//
//  foodDetailCell.swift
//  neufood
//
//  Created by huangminmin on 2017/11/7.
//  Copyright © 2017年 huangminmin. All rights reserved.
//

import UIKit

class foodDetailCell: UITableViewCell {

    @IBOutlet weak var telNum: UILabel!
    
    @IBOutlet weak var collectionBtn: UIButton!
    var phonenum: NSString!{
        willSet(newValue) {
            telNum.text = newValue as String;
        }
    }
    var food : ROFoodModel! {
        didSet(oldValue){
            self.isCollect()
            let parameters: NSDictionary = ["food_id":food.food_id]
            RONetworkMngTool.sharedNetWorkMngTool().RONetwork_getShopInformationWithParameters(parameters, view:UIView()) { (phonenum) in
                self.phonenum = phonenum
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    @IBAction func makeCall(_ sender: UIButton) {
        //真机测试
        let url = NSURL(string: "tel://" + (phonenum as String
            ))
        UIApplication.shared.open(url! as URL, options:[:] , completionHandler: nil)
    }
    
    @IBAction func order(_ sender: UIButton) {
         NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Order"), object: nil)
    }
    @IBAction func collection(_ sender: UIButton) {
        let parameters1: NSDictionary = ["user_id": userId,"food_id": self.food.food_id] as NSDictionary
        RONetworkMngTool.sharedNetWorkMngTool().RONetwork_collectFoodWithParameters(parameters1, view: UIView.init()) { (success) in
            if success == "1" {
                self.isCollect()
            } else {
                print("收藏/取消收藏失败！")
            }
        }
    }
    //判断是否收藏
    func isCollect() {
        let parameters2: NSDictionary = ["user_id": userId,"shop_food_id": self.food.food_id,"flag": 1] as NSDictionary
        RONetworkMngTool.sharedNetWorkMngTool().RONetwork_isCollectedWithParameters(parameters2, view: UIView.init()) { (isCollect) in
            if isCollect == "0" {
                self.collectionBtn.setTitle("收藏", for: UIControlState.normal)
            }else if isCollect == "1" {
                self.collectionBtn.setTitle("取消收藏", for: UIControlState.normal)
            }
        }
    }
    
    class  func  creatCell(_ tableView : UITableView) -> foodDetailCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "foodDetailCell")
        if  cell == nil {
            cell = (Bundle.main.loadNibNamed("foodDetailCell", owner: nil, options: nil))?.first as! foodDetailCell
        }
        return cell as! foodDetailCell;
    
    
    


    }
}
